package com.jbinfo.qingcaowan.studyManager.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jbinfo.common.utils.DataTables;
import com.jbinfo.common.utils.UUIDUtils;
import com.jbinfo.qingcaowan.common.BusinessException;
import com.jbinfo.qingcaowan.common.ExceptionConstants.BusinessExceptionCode;
import com.jbinfo.qingcaowan.security.entity.User;
import com.jbinfo.qingcaowan.studyManager.dao.RemarkDao;
import com.jbinfo.qingcaowan.studyManager.dto.RemarkModel;
import com.jbinfo.qingcaowan.studyManager.entity.Remark;
import com.jbinfo.qingcaowan.studyManager.service.RemarkService;
import com.jbinfo.qingcaowan.user.dao.ScoreDetailDAO;
import com.jbinfo.qingcaowan.user.dao.ScoreSetDAO;
import com.jbinfo.qingcaowan.user.dao.UserDao;
import com.jbinfo.qingcaowan.user.domain.ScoreDetailDomain;

/**
 * 
* @ClassName: remarkServiceImpl 
* @Description: TODO 用户管理service实现类
* @author sunxinyu
* @date 2016年6月27日 上午10:23:02
 */
@Service("remarkService")
public class RemarkServiceImpl implements RemarkService {

	@Resource(name="remarkDao")
	private RemarkDao remarkDao;
	
	@Resource(name="scoreSetDAO")
	private ScoreSetDAO scoreSetDAO;
	
	@Resource(name="userDao")
	private UserDao userDao;
	
	@Resource(name="scoreDetailDAO")
	private ScoreDetailDAO scoreDetailDAO;

	@Override
	public DataTables<Remark> findAllRemark(RemarkModel remarkModel) {
		List<Remark> tList=remarkDao.findAllRemark(remarkModel);
		return DataTables.combinTotalRecords(tList);
	}

	@Override
	public RemarkModel getById(String remarkId) {
		
		return  (RemarkModel) remarkDao.selectByPrimaryKey(remarkId);
	}

	@Override
	public DataTables<Remark> findByUser(RemarkModel remarkModel) {
		List<Remark> tList=remarkDao.findByUser(remarkModel);
		return DataTables.combinTotalRecords(tList);
	}

	@Override
	public void changeShow(RemarkModel remarkModel) {
		
		remarkDao.changeShow(remarkModel);
	}
	@Override
	public void changeShow1(RemarkModel remarkModel) {
		
		remarkDao.changeShow1(remarkModel);
	}

	@Override
	public  List<RemarkModel> findBookList(RemarkModel remarkModel) {
		
		return (List<RemarkModel>) remarkDao.findBookList(remarkModel);
				}

	@Override
	public List<RemarkModel> findChapterList(String bookid) {
		
		return  remarkDao.findChapterList(bookid);
	}

	@Override
	public List<RemarkModel> findCardList(String chapterid) {
		
		return remarkDao.findCardList(chapterid);
	}

	// 保存评论信息
	public Object saveRemarkInfo(Remark remark) {
		
		if (remark.getUserid() == null || remark.getUserid().isEmpty()) {
			throw new BusinessException(BusinessExceptionCode.param_error, "用户ID不能为空");
		}
		// 根据userId 获取orgId
		String orgId = "";
		String userId = remark.getUserid();
		User user = (User) userDao.selectByPrimaryKey(userId);
		orgId = user.getUserOrg();
		
		remark.setRemarkId(UUIDUtils.create());
		remark.setRemarkTime(new Date());
		// 默认显示
		remark.setIsshow("1");
		remarkDao.insertSelective(remark);
		
		// 插入学分详情表
		String type = "0003";
		Integer score = scoreSetDAO.findScoreByType(type);
		
		ScoreDetailDomain entity = new ScoreDetailDomain();
		entity.setId(UUIDUtils.create());
		entity.setType(type);
		entity.setOrgId(orgId);
		entity.setUserId(userId);
		entity.setScore(score);
		entity.setCreatUser(userId);
		entity.setCreatTime(new Date());
		entity.setModifyUser(userId);
		entity.setModifyTime(new Date());
		
		scoreDetailDAO.save(entity);

		return null;
	}
	
	// 获取评论数
	public Object getRemarkCnt(Remark remark) {
		String strCnt = "";
		
		Integer nCnt = remarkDao.findRemarkCntByParam(remark);
		strCnt = nCnt.toString();
		
		return strCnt;
	}
	
	// 
	/*@Override
	public DataTables<User> findByParam(UserModel userModel) {
		List<User> tList = userDao.findByParam(userModel);
		return DataTables.combinTotalRecords(tList);
	}
*/
}
